package com.yonyou.gmcc.gateway.wechat.exception;

import org.apache.log4j.Logger;
import org.marketing.common.hystrix.MarketingAppException;
import org.marketing.common.hystrix.MarketingBizException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

//@ControllerAdvice
public class ExceptionHandlerAdvice {
	private static Logger logger=Logger.getLogger(ExceptionHandlerAdvice.class);
	
	/**
	*
	* 网关断路器异常捕获
	* @date 2016年12月5日
	* @param exception
	* @param req
	* @return
	* @throws Exception
	*/
	@ExceptionHandler(value=Exception.class)
	//@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
	public @ResponseBody String exception(Exception exception,WebRequest req) throws Exception{
		logger.info("-----------ExceptionHandlerAdvice----------exception---"+exception.getMessage());
		ServletWebRequest r=(ServletWebRequest)req;
		//byte[] b="xxxx---uyyy".getBytes();
		//r.getResponse().getOutputStream().write(b);
		//捕捉断路器方法抛出的异常，设置http状态为 460
		if(exception.getMessage().contains("fallback failed")){
			if(exception.getCause()!=null){
				r.getResponse().setStatus(460);
				return exception.getCause().getMessage();
			}
		}
		r.getResponse().setStatus(500);
		return exception.getMessage();
	}
	
	
    /**
    *
    * 网关业务异常捕获
    * @date 2016年12月5日
    * @param exception
    * @param req
    * @return
    * @throws Exception
    */
    @ExceptionHandler(value=MarketingBizException.class)
    @ResponseStatus(HttpStatus.EXPECTATION_FAILED)
    public @ResponseBody String exception417(Exception exception,WebRequest req) throws Exception{
        String tmp="";
        //捕捉微服务抛出的异常，设置http状态为 417
        //原理(MyErrorDecoder捕捉微服务异常后，抛出MarketingBizException异常，这里处理返回客户端)
        try{
            ServletWebRequest r=(ServletWebRequest)req;
            logger.info("-----------ExceptionHandlerAdvice----------exception---"+exception.getMessage());
            //exception.printStackTrace();
            tmp=exception.getMessage();
            //logger.info("---------------------DemoException---"+tmp);
        }catch(Exception ex){
            tmp="unknow exception";
        }
        return tmp;
    }
    
    /**
    *
    * 网关系统异常捕获
    * @date 2016年12月5日
    * @param exception
    * @param req
    * @return
    * @throws Exception
    */
    @ExceptionHandler(value=MarketingAppException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public @ResponseBody String exception500(Exception exception,WebRequest req) throws Exception{
        String tmp="";
        //捕捉微服务抛出的异常，设置http状态为 417
        //原理(MyErrorDecoder捕捉微服务异常后，抛出MarketingBizException异常，这里处理返回客户端)
        try{
            logger.info("-----------ExceptionHandlerAdvice----------exception---"+exception.getMessage());
            //exception.printStackTrace();
            tmp=exception.getMessage();
            //logger.info("---------------------DemoException---"+tmp);
        }catch(Exception ex){
            tmp="unknow exception";
        }
        return tmp;
    }
	
}
